package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.UserAddr;
import com.powernode.service.UserAddrService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@Api(tags = "会员收获地址管理")
@RequestMapping("p/address")
public class UserAddrController {

   @Autowired
   private UserAddrService userAddrService;

   @GetMapping("list")
   @ApiOperation("查询当前会员的收货地址列表")
   public ResponseEntity<List<UserAddr>> getUserAddrList() {
      String openId = AuthUtil.getOpenId();
      List<UserAddr> userAddrs = userAddrService.selectUserAddrList(openId);
      return ResponseEntity.ok(userAddrs);

   }

   @PostMapping
   @ApiOperation("新增会员的收货地址")
   public ResponseEntity<Void> addUserAddr(@RequestBody UserAddr userAddr) {
      String openId = AuthUtil.getOpenId();
      userAddr.setUserId(openId);
      userAddrService.saveUserAddr(userAddr);
      return ResponseEntity.ok().build();
   }

   @PutMapping("defaultAddr/{id}")
   @ApiOperation("修改会员的默认收货地址")
   public ResponseEntity<Void> changeDefaultAddr(@PathVariable("id") Long id) {
      String openId = AuthUtil.getOpenId();
      userAddrService.changeDefaultAddr(openId, id);
      return ResponseEntity.ok().build();
   }

   @GetMapping("getUserDefaultAddr")
   @ApiOperation("提供远程调用查询会员的默认收货地址")
   UserAddr getUserDefaultAddr(@RequestParam String openId) {
      return userAddrService.getOne(new LambdaQueryWrapper<UserAddr>()
              .eq(UserAddr::getUserId, openId)
              .eq(UserAddr::getCommonAddr, 1)
      );
   }


}
